package com.flashcash.kotlin.activity

import android.content.Intent
import android.os.Build
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.*
import com.flashcash.solu.BuildConfig
import com.flashcash.solu.R
import com.flashcash.solu.app.base.BaseActivity
import com.flashcash.solu.app.base.BaseActivityView
import com.flashcash.solu.app.base.presenter.BaseActivityPresenter
import com.flashcash.solu.app.base.presenter.BaseActivityPresenterImpl
import com.flashcash.solu.common.FieldParams
import com.flashcash.solu.view.IntroduceActivity
import com.flashcash.solu.widget.xleotoast.XLeoToast
import com.x.leo.apphelper.documented.DocumentMessage
import com.x.leo.apphelper.log.XLog
import org.jetbrains.anko.find

/**
 * Created by XLEO on 2018/1/12.
 */
class PolicyDialogActivity : BaseActivity<PolicyDialogActPresenter>(), PolicyDialogActView {
    override fun getLayoutResId(): Int {
        return R.layout.dialog_privary_policy
    }

    override fun init() {
        try {
            find<ImageButton>(R.id.id_imagebutton_back).visibility = View.GONE
            find<ImageButton>(R.id.id_imagebutton_info_list).visibility = View.GONE
        } catch (e: Exception) {
            XLog.e(e.message ?: "", e, 100)
        }
        find<TextView>(R.id.id_textview_title).text = getString(R.string.textview_me_policy)
        val cb = findViewById<CheckBox>(R.id.cb_aggreed)
        val bt = findViewById<Button>(R.id.btn_ok)
        val iv = findViewById<ImageView>(R.id.iv_about)
        bt.setOnClickListener {
            if (!cb.isChecked) {
                XLeoToast.showMessage(R.string.check_the_checkbox)
            } else {
                DocumentMessage.getDoc().putBoolean(FieldParams.HAS_AGREED_THE_POLICY, true).reset()
                //同意完协议,未登录，跳转到新首页
                startActivity(Intent(this, IntroduceActivity::class.java))
                finish()
            }
        }
        bt.isSelected = true
        cb.setOnCheckedChangeListener { buttonView, isChecked ->
            if (!cb.isClickable) {
                cb.isSelected = false
                bt.isSelected = true
            } else if (isChecked) {
                cb.isSelected = true
                bt.isSelected = false
            } else {
                cb.isSelected = false
                bt.isSelected = true
            }
        }
        val wv = findViewById<WebView>(R.id.wv)
        wv.settings.setAppCachePath(cacheDir.absolutePath)
        wv.settings.setAppCacheEnabled(true)
        wv.settings.displayZoomControls = true
        wv.settings.builtInZoomControls = true
        wv.settings.javaScriptEnabled = true
        wv.settings.setSupportZoom(false)
        wv.settings.minimumFontSize = 14
        wv.scrollBarStyle = View.SCROLLBARS_OUTSIDE_INSET
        wv.isVerticalScrollBarEnabled = true
        wv.overScrollMode = View.OVER_SCROLL_ALWAYS
        wv.setWebChromeClient(object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (newProgress > 90 && iv.visibility == View.VISIBLE) {
                    iv.visibility = View.GONE
                }
            }
        })
        wv.setWebViewClient(object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                wv.visibility = View.VISIBLE
                iv.visibility = View.GONE
            }
        })
        wv.loadUrl(BuildConfig.AGREEMENT_URL)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            cb.isClickable = false
            wv.setOnScrollChangeListener { v, _, scrollY, _, _ ->
                val contentHeight = (wv.contentHeight * wv.scale - wv.height).toInt()
                if (v.scrollBarSize + scrollY >= contentHeight - 20) {
                    cb.isClickable = true
                }
            }
        }
    }

    override fun initPresenterImpl(): PolicyDialogActPresenter {
        return PolicyDialogActPreImpl()
    }

}

class PolicyDialogActPreImpl : PolicyDialogActPresenter, BaseActivityPresenterImpl() {

}

interface PolicyDialogActView : BaseActivityView {

}

interface PolicyDialogActPresenter : BaseActivityPresenter {

}
